﻿// 项目：	Guan.CMS.Model - Guan.CMS
// 文件名：	User.cs
// 
// 修改时间：2013-03-06 20:39
// 修改人：	Cooclsee

using System;
using System.ComponentModel.DataAnnotations;
using System.Text;
using Guan.CMS.Core.Common;

namespace Guan.CMS.Model.Entities
{
    public class User
    {
        public User()
        {
            this.DateCreated = DateTime.Now;
        }

        [Key]
        public int UserId { get; set; }

        [Required]
        public string Email { get; set; }

        [Required]
        public string Name { get; set; }
        public string UserName { get; set; }

        public string PasswordHash { get; set; }
        public DateTime DateCreated { get; set; }
        public DateTime? LastLoginTime { get; set; }
        public bool Activated { get; set; }
        public int RoleId { get; set; }

        public string Password
        {
            set { this.PasswordHash = Md5Encrypt.Md5EncryptPassword(value); }
        }

        internal static string GenerateRandomString()
        {
            var builder = new StringBuilder();
            var random = new Random();
            for (int i = 0; i < 6; i++)
            {
                char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(25*random.NextDouble() + 75)));
                builder.Append(ch);
            }
            return builder.ToString();
        }

        public string ResetPassword()
        {
            var newPass = GenerateRandomString();
            this.Password = newPass;
            return newPass;
        }

        public string DisplayName
        {
            get { return this.Name; }
        }
    }
}